package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.EmsGridRecordGroup;
import com.econ.powercloud.ems.dal.mapper.EmsGridRecordGroupMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmsGridRecordGroupDAO extends ServiceImpl<EmsGridRecordGroupMapper, EmsGridRecordGroup> {

    private final String LIMIT_ONE = "limit 1";

    public EmsGridRecordGroup selectByRecordIdAndOrderNum(Long recordId, Integer orderNum) {
        LambdaQueryWrapper<EmsGridRecordGroup> wrapper = Wrappers.<EmsGridRecordGroup>lambdaQuery()
                .eq(EmsGridRecordGroup::getRecordId, recordId).eq(EmsGridRecordGroup::getOrderNum, orderNum);
        return baseMapper.selectOne(wrapper);
    }

    public List<EmsGridRecordGroup> selectByRecordId(Long recordId) {
        LambdaQueryWrapper<EmsGridRecordGroup> wrapper = Wrappers.<EmsGridRecordGroup>lambdaQuery().eq(EmsGridRecordGroup::getRecordId, recordId);
        return baseMapper.selectList(wrapper);
    }

    public EmsGridRecordGroup selectLatestByRecordId(Long recordId) {
        LambdaQueryWrapper<EmsGridRecordGroup> wrapper = Wrappers.<EmsGridRecordGroup>lambdaQuery()
                .eq(EmsGridRecordGroup::getRecordId, recordId).orderByDesc(EmsGridRecordGroup::getId).last(LIMIT_ONE);
        return this.baseMapper.selectOne(wrapper);
    }
}
